/**
 * Created by cchen on 2015/6/8.
 */

'use strict';

angular.module('backofficeApp.bannerOrder.controllers', []).
    controller('bannerOrderCtrl', ['$scope', '$routeParams', 'CommunityService', '$http', '$location', function ($scope, $routeParams, communityService, $http, $location) {

        $scope.bannerType = [
            {
                name: 'banner',
                value: 1
            },
            {
                name: '话题公告',
                value: 2
            }
        ];

        $scope.communityId = $routeParams.communityId;
        $scope.type = Number($routeParams.type);

        $scope.communityLabelList = communityService.getAllCommunityService();

        $scope.getCommunityBanner = function () {

            $location.path('/bannerOrder/' + $scope.type + '/' + $scope.communityId)
        };

        $scope.getData = function () {
            $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/banner/getCommunityBannerList/' + $scope.type + '/' + $scope.communityId, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.bindList = data.data;
            }).error(function () {
            });
        };

        if ($scope.communityId != null) {
            $scope.getData($scope.communityId);
        }

        $scope.upNews = function (index) {
            if (index > 0) {
                var temp = $scope.bindList[index]
                $scope.bindList[index] = $scope.bindList[index - 1]
                $scope.bindList[index - 1] = temp;

                console.debug($scope.bindList);
            }
        };
        $scope.downNews = function (index) {
            if (index < $scope.bindList.length - 1) {
                var temp = $scope.bindList[index];
                $scope.bindList[index] = $scope.bindList[index + 1]
                $scope.bindList[index + 1] = temp;
                console.debug($scope.bindList);
            }
        };

        $scope.setOrder = function () {
            var bindIdList = new Array();
            $scope.bindList.forEach(function (news) {
                bindIdList.push(news.objId);
            });

            var temp = {
                "bindId": bindIdList
            };

            console.debug(temp);
            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/banner/setBannerBindOrder', temp, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.getData();
            }).error(function () {

            });
        };


        $scope.bannerDelete = function (bindId) {
            $http.delete('http://' + backOfficeHost + '/rest/v1.0/backoffice/banner/deleteBannerBind/' + bindId, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.getData();
            }).error(function () {

            });
        };

        $scope.setDisplayOrder = function (bindId) {
            bootbox.prompt('设置顺序（数字越大越靠前）', function (result) {
                if (result != null && result != '') {
                    if (!/^(\+|-)?(\d+)(\.\d*)?$/g.test(result)) {
                        alert('只允许输入数字');
                        return false;
                    }

                    var opt = {
                        bindId: bindId,
                        displayOrder: Number(result)
                    };
                    $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/banner/setBindBannerOrder', opt, {
                        headers: {'Content-Type': 'application/json;charset=utf-8'}
                    }).success(function (result) {
                        $scope.getData($scope.communityId);
                    }).error(function () {
                    });
                }
            }).find("div.modal-dialog").css({width: '30%'});
        }

        $scope.dataView = function (bind){

            console.debug(bind);
            switch (bind.bannerId.openType){
                case 1:
                    //问答详情
                    $location.path('/questionManager/edit/'+ bind.bannerId.contentId +'/all/all/all/all/empty/empty/empty/empty/all/empty');
                    //$state.go('questionManager/edit/'+ bind.bannerId.contentId +'/all/all/all/all/empty/empty/empty/empty/all/empty');
                    break;
                case 2:
                    //公告详情
                    $location.path('/notice/'+ bind.bannerId.contentId);
                    break;
                case 3:


                    break;
                case 4:
                    //专题
                    $location.path('/topicEdit/'+ bind.bannerId.contentId);

                    break;
                case 6:
                    //邻里活动详情

                    break;

                default :


            }



        };


    }]);